package com.example.springeventlistenersample.controller;

import com.example.springeventlistenersample.AsnycTaskDealer;
import com.example.springeventlistenersample.SpringEvent1;
import com.example.springeventlistenersample.UserInfo;
import com.example.springeventlistenersample.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;

@RestController
@RequestMapping
public class ReigsterController {
    Logger log = LoggerFactory.getLogger(ReigsterController.class);

    @Autowired
    UserService userService;



    @Autowired
    WebApplicationContext webApplicationContext;
    @RequestMapping(value = {"register1"}, method = {RequestMethod.GET})
    public String register1(String name,Integer age) {
        UserInfo info = new UserInfo();
        info.setName(name);
        info.setAge(age);
        log.info("ReigsterController注册用户完成");
        webApplicationContext.publishEvent(new SpringEvent1(this,info));
        return "注册成功2";
    }



    @RequestMapping(value = {"register"},method = {RequestMethod.GET})
    public String register(String name,Integer age){
        UserInfo info = new UserInfo();
        info.setName(name);
        info.setAge(age);
        log.info("ReigsterController注册用户完成");
        userService.publish(info);
        return "注册成功";
    }

    @Autowired
    AsnycTaskDealer asnycTaskDealer;
    @RequestMapping(value = {"asynctask"}, method = {RequestMethod.GET})
    public String asynctask() {
        log.info("准备异步任务方法执行,所在线程名称："+Thread.currentThread().getName());
        asnycTaskDealer.exec();
        asnycTaskDealer.exec();
        asnycTaskDealer.exec();
        asnycTaskDealer.exec();
        return "异步任务调度成功";
    }


}
